Sanakey
2021-07-09 3fc241d02fb8b671289de3d9d80bf848349c04d4
提交 | 用户 | age
3fc241 1 import * as dayjs from "dayjs";
S 2 const formatTime = (dateString: Date,format: string ='YYYY-MM-DD HH:mm') => {
3   return dayjs(dateString).format(format);
67dc7b 4 }
S 5
3fc241 6 // const formatTime2 = (dateString: Date) => {
S 7 //   let date = new Date(dateString);
8 //   const year = date.getFullYear()
9 //   const month = date.getMonth() + 1
10 //   const day = date.getDate()
11 //   const hour = date.getHours()
12 //   const minute = date.getMinutes()
13 //   const second = date.getSeconds()
14 //
15 //   return (
16 //     [year, month, day].map(formatNumber).join('-') +
17 //     ' ' +
18 //     [hour, minute, second].map(formatNumber).join(':')
19 //   )
20 // }
21 //
22 // const formatNumber = (n: number) => {
23 //   const s = n.toString()
24 //   return s[1] ? s : '0' + s
25 // }
aa6d80 26
S 27 const to = (promise:Promise<any>)=>{
28   return promise.then((res:any)=>{
29     return [null,res]
30   }).catch((error:any)=>{
31     return [error]
32   })
33 }
34
3dd7c0 35 const getUrl = (url:string,params:string)=>{
S 36   // AppId: app.globalData.AppId
37   url = wx.globalData.reqBase + url;
38   if(params != null){
39     url = url + (url.indexOf('?') >= 0 ? "&" : "?") + params;
40   }
41   if (wx.globalData.AppId != null && url.indexOf('AppId=') < 0) {
42     url = url + (url.indexOf('?') >= 0 ? "&" : "?") + "AppId=" + wx.globalData.AppId;
43   }
44   if (url.indexOf('wx=3') < 0) {
45     url = url + (url.indexOf('?') >= 0 ? "&" : "?")  +'wx=3';
46   }
282d69 47   // if (url.indexOf('FromUserId=') < 0) {
S 48   //   url = url + (url.indexOf('?') >= 0 ? "&" : "?")  +'FromUserId=' + wx.globalData.FromUserId;
49   // }
3dd7c0 50
S 51   return url ;
52 }
53
a38dad 54 const makePhoneCall = (mobile:string)=>{
S 55   wx.pro.showModal({
56     title: '提示',
57     content: `即将拨打电话 ${mobile}`,
58   }).then((res:any) => {
59     // on close
60     console.log(res);
61     if (res.confirm){
62       console.log('确认');
63       wx.makePhoneCall({
64         phoneNumber: mobile,
65       })
66     }
67   });
68 }
69
3fc241 70 const debounce = (fn:()=>{},delay:number = 200)=>{
f9ad6c 71   let timer:any = null;
S 72   return function () {
73     if (timer) clearTimeout(timer);
74     timer = setTimeout(() => {
75       // console.log(this);
76       if (typeof fn === "function") {
77         // @ts-ignore
78         fn.apply(this,arguments)
79       }
80       timer = null;
81     }, delay);
82   }
83 }
84
58c889 85 const  setLoginData = (_this:any)=>{
8724a4 86   let loginData = wx.globalData.loginData;
S 87   console.log('<<<<<<<<<获取loginData',loginData);
88   _this.setData({
89     loginData,
90   })
91 }
d8c37e 92
S 93 const getSingleUploadImageList = (data:any,type:string)=>{
94   let [unid,seq] = data[type].split(";");
95   return [{
96     url: data[`${type}Url`]||'',
97     unid,
98     seq
99   }]
100 }
9d3461 101
S 102 //获取微信定位
103 const getLocation =  async ()=>{
104   let isAuthorize = await getAuthorizeInfo('scope.userLocation');
105   if (!isAuthorize) {
106     let [error, result] = await wx.$utils.to(wx.pro.authorize({scope: 'scope.userLocation'}));
107     if (error){
108       console.log(error);
109       return false;
110     }
111     console.log('授权弹窗信息',result);
112     // wx.showModal({
113     //   title: "提示",
114     //   content: "您还未授权定位,请在接下来打开的设置页面开启相册定位!",
115     //   success(res) {
116     //     if (res.confirm) {
117     //       console.log("用户点击确定");
118     //       wx.openSetting();
119     //     } else if (res.cancel) {
120     //       console.log("用户点击取消");
121     //     }
122     //   },
123     // });
124     return;
125   }
126   let [error, result] = await wx.$utils.to(
127     wx.pro.getLocation()
128   )
129   if (error){
130     console.log(error);
131     return false;
132   }
133   console.log('获取定位信息',result);
134   wx.globalData.location = {
135     latitude:result.latitude,
136     longitude:result.longitude
137   };
138   // this.setData({
139   //   latitude:result.latitude,
140   //   longitude:result.longitude
141   // })
142   return true;
143 }
144
145 const getAuthorizeInfo = async(scope:string="scope.writePhotosAlbum")=>{
146   let [err, res] = await wx.$utils.to(wx.pro.getSetting());
147   if (err) {
148     return false;
149   }
150   console.log("getSetting", res);
151   return res.authSetting[scope] !== false;
152 }
58c889 153
S 154 // 设置门店名称
155 const setShopName = (_this:any)=>{
156   let settings = wx.globalData.settings;
157   console.log('<<<<<<<<<获取settings.name',settings.name);
158   _this.setData({
159     shopName:settings.name,
a3c13b 160     helpUrl:settings.helpUrl,
58c889 161   })
S 162   wx.setNavigationBarTitle({
163     title:settings.name
164   })
a3c13b 165 }
S 166
167 const watchObj = (target:object,callback:Function)=>{
168   let handler = {
169     get(target:object, key:string, receiver:any) {
170       console.log('>>>>>>...get', key)
171       return Reflect.get(target, key, receiver)
172       //等价于 return target[key];
173     },
174     set(target:object, key:string, value:any, receiver:any) {
175       console.log('>>>>>...set', key, value);
176       typeof callback === 'function' && callback(key, value)
177       return Reflect.set(target, key, value, receiver)
178       //等价于 return target[key] = value;
179     }
180   }
181   return new Proxy(target, handler)
182
58c889 183 }
3fc241 184
S 185 const scrollToError = (selector:string, errorMsg:string, duration:number = 3000)=>{
186   wx.pageScrollTo({
187     selector,
188     duration: 300
189   })
190   wx.showToast({
191     title: errorMsg,
192     icon: 'none',
193     duration
194   })
195 }
aa6d80 196 export default{
S 197   formatTime,
3dd7c0 198   to,
S 199   getUrl,
f9ad6c 200   makePhoneCall,
8724a4 201   debounce,
58c889 202   setLoginData,
9d3461 203   getSingleUploadImageList,
S 204   getLocation,
58c889 205   getAuthorizeInfo,
a3c13b 206   setShopName,
3fc241 207   watchObj,
S 208   scrollToError
aa6d80 209 }