request.js 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. const app = getApp();
  2. const env = {
  3. NODE_ENV: 'dev',
  4. dev: {
  5. apiUrl: 'https://pkapi.zhiqiyun.com'
  6. },
  7. prd: {
  8. apiUrl: 'https://pkapi.pikadongli.com'
  9. },
  10. pota: {
  11. // apiUrl: 'http://192.168.110.182:8098' ,//zf
  12. apiUrl: 'http://192.168.110.180:8098' ,//wjg
  13. }
  14. }
  15. var header = {
  16. 'content-type': 'application/x-www-form-urlencoded',
  17. 'appId': 'ZQ4OItRePFqbge5', //享团
  18. }
  19. /**小程序配置 */
  20. const publics = {
  21. picUrls: 'https://zhiqiyun.oss-cn-hangzhou.aliyuncs.com/static/', //图片地址
  22. 'copyrighttitle': '智企云', //底部版权公司名称
  23. 'copyrighttel': '400-698-5980', //底部版权服务电话
  24. 'copyrightlogo': 'zhiqiyun', //底部版权 logo 文件名
  25. 'yaomeng': 'https://cfwz.yaomengwang.cn/hunan?enterpriseCode=hnty&orderId=', //药盟接口地址
  26. 'storemanagers': 'https://websmall.zhiqiyun.com?appId=', //saas门店管理地址(智企云所有)
  27. 'channel': '', //处方中间渠道号 SAAS系统为空
  28. 'is_customer_pick_up': false,
  29. 'mapLBSKEY': 'I52BZ-OXYKJ-PWMFN-XWADQ-WR775-XTBQE',
  30. 'iconSrc': 'red/',
  31. 'titleTopBgColor': '#EB692F',
  32. }
  33. /*
  34. * 获取token
  35. */
  36. const getToken = (success) => {
  37. let token = getStorage('AUTH_TOKEN');
  38. if (token) {
  39. success.call(this, token);
  40. return false;
  41. } else {
  42. loadToken(data => {
  43. success.call(this, data);
  44. });
  45. }
  46. }
  47. const loadToken = (success) => {
  48. let that = this
  49. // uni.request({
  50. // url: env[env.NODE_ENV].apiUrl + '/api/access_token',
  51. // data: {},
  52. // method: 'get',
  53. // header: header,
  54. // success(json) {
  55. // if (json.statusCode !== 200) return msg(json.data.message);
  56. // if (json.data.code !== 0) return msg(json.data.msg)
  57. // setStorage('AUTH_TOKEN', json.data.data);
  58. // success.call(that, json.data.data);
  59. // },
  60. // fail() {
  61. // msg('access_token 获取失败');
  62. // }
  63. // })
  64. console.log('token')
  65. let token = getStorage('AUTH_TOKEN');
  66. if (token) {
  67. success.call(this, token);
  68. return false;
  69. } else {
  70. // msg('access_token 获取失败');
  71. removeStorage('userInfo')
  72. // return redirectIndex();
  73. }
  74. }
  75. /**
  76. * 微信小程序静默授权登录
  77. * @param {*} parentId
  78. * @param {*} merchantId
  79. */
  80. const silenceLogin = (parentId, merchantId) => {
  81. // #ifdef MP-WEIXIN
  82. if (!isLogins(false)) { //未登录
  83. uni.login({
  84. success(res) {
  85. console.log('静默授权登录>>>', res);
  86. let params = {
  87. code: res.code,
  88. parentId: parentId ? parentId : '',
  89. merchantId: merchantId ? merchantId : ''
  90. }
  91. return new Promise((resolve, reject) => {
  92. postRequest('/api/v3/silence/login', params, data => {
  93. if(data.token) setStorage('AUTH_TOKEN',data.token);
  94. getRequest('/api/user/my', {}, suc => {
  95. data.isDistriUser = suc.isDistriUser;
  96. setStorage('userInfo', data);
  97. });
  98. resolve();
  99. });
  100. })
  101. }
  102. });
  103. }
  104. // #endif
  105. }
  106. const getWXUserProfile = (suc) => {
  107. let that = this;
  108. var sessionKey = '';
  109. var openid = '';
  110. getApp().globalData.getCheckSessoin(json => {
  111. sessionKey = json.session_key;
  112. openid = json.openid;
  113. });
  114. try {
  115. uni.getUserProfile({
  116. desc: '完善信息',
  117. success: res => {
  118. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  119. },
  120. fail: res => {
  121. console.log('授权失败数据>>>', res);
  122. msg(res);
  123. }
  124. });
  125. } catch (e) {
  126. // 老版
  127. wx.getUserInfo({
  128. success: function(ress) {
  129. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  130. }
  131. });
  132. }
  133. }
  134. /**
  135. * 保存微信头像
  136. */
  137. const updataWxInfos = (iv, encryptedData, sessionKey, openid, suc) => {
  138. if (!encryptedData || !iv) return false;
  139. let _params = {
  140. sessionKey: sessionKey,
  141. openId: openid,
  142. iv: iv,
  143. encryptedData: encryptedData
  144. };
  145. postRequest('/api/user/weixin/update', _params, json => {
  146. setStorage('userInfo', json);
  147. suc.call();
  148. });
  149. }
  150. const clearValueEmpty = (data) => {
  151. let keyValue = {};
  152. for (let key in data) {
  153. let value = typeof data[key];
  154. if (value == 'string' && value) {
  155. if (data[key] != 'undefined' && data[key] != " " && data[key] != undefined && data[key] != null) {
  156. keyValue[key] = data[key];
  157. }
  158. // keyValue[key] = data[key];
  159. } else if (value == 'number' && value != null) {
  160. keyValue[key] = data[key];
  161. } else if (value == 'boolean') {
  162. keyValue[key] = data[key];
  163. } else {
  164. if (data[key]) keyValue[key] = data[key];
  165. }
  166. }
  167. return keyValue;
  168. }
  169. /**
  170. * 公共请求
  171. *
  172. * @param {*} url 请求url
  173. * @param {*} data 请求参数
  174. * @param {*} method 请求方法
  175. * @param {*} success 成功函数
  176. * @param {*} isLoad 是否显示加载提示
  177. */
  178. const baseRequest = (url, data, method, success, isLoad) => {
  179. if(url=='/admin/v2/login'||url=='/api/protocol'||url=='/api/params/value'||url=='/api/other/config'||url=='/admin/v2/user/feedback'){
  180. if (!isLoad) uni.showNavigationBarLoading();
  181. if (url.indexOf('/v1') != -1) {
  182. header['content-type'] = 'application/x-www-form-urlencoded'
  183. } else {
  184. header['content-type'] = 'application/json;charset=UTF-8'
  185. }
  186. uni.request({
  187. url: env[env.NODE_ENV].apiUrl + url,
  188. data: clearValueEmpty(data),
  189. method: method,
  190. header: header,
  191. success(json) {
  192. if (!isLoad) uni.hideNavigationBarLoading();
  193. if (json.statusCode !== 200) return msg(json.data.message);
  194. if (json.data.code === 10021 || json.data.code === 10020) {
  195. // console.log("第二次执行")
  196. removeStorage('AUTH_TOKEN');
  197. return redirectIndex();
  198. }
  199. if (json.data.code !== 0) {
  200. return msg(json.data.msg)
  201. }
  202. // console.log("第二次执行")
  203. let data = json.data.data;
  204. if (typeof data === 'string' && data.indexOf("{") === 0) {
  205. data = JSON.parse(data);
  206. }
  207. success.call(this, data);
  208. },
  209. fail() {
  210. if (!isLoad) uni.hideNavigationBarLoading();
  211. },
  212. })
  213. }
  214. else{
  215. getToken(token => {
  216. if (!isLoad) uni.showNavigationBarLoading();
  217. header.authorityToken = token;
  218. if (url.indexOf('/v1') != -1) {
  219. header['content-type'] = 'application/x-www-form-urlencoded'
  220. } else {
  221. header['content-type'] = 'application/json;charset=UTF-8'
  222. }
  223. uni.request({
  224. url: env[env.NODE_ENV].apiUrl + url,
  225. data: clearValueEmpty(data),
  226. method: method,
  227. header: header,
  228. success(json) {
  229. if (!isLoad) uni.hideNavigationBarLoading();
  230. if (json.statusCode !== 200) return msg(json.data.message);
  231. if (json.data.code === 10021 || json.data.code === 10020) {
  232. // console.log("第二次执行")
  233. removeStorage('AUTH_TOKEN');
  234. removeStorage('userInfo');
  235. return redirectIndex();
  236. }
  237. if (json.data.code !== 0) {
  238. return msg(json.data.msg)
  239. }
  240. // console.log("第二次执行")
  241. let data = json.data.data;
  242. if (typeof data === 'string' && data.indexOf("{") === 0) {
  243. data = JSON.parse(data);
  244. }
  245. success.call(this, data);
  246. },
  247. fail() {
  248. if (!isLoad) uni.hideNavigationBarLoading();
  249. },
  250. // complete(){
  251. // success.call(this, data);
  252. // },
  253. })
  254. })
  255. }
  256. }
  257. const loadIng = (msg) => {
  258. uni.showLoading({
  259. title: msg,
  260. mask: true
  261. })
  262. }
  263. /**POST请求 */
  264. const postRequest = (url, data, success, isLoad) => {
  265. header['content-type'] = 'application/json;charset=UTF-8'
  266. baseRequest(url, data, 'post', success, isLoad);
  267. }
  268. const postRequestLoding = (url, data, success, isLoding) => {
  269. header['content-type'] = 'application/json;charset=UTF-8'
  270. baseRequest(url, data, 'post', success, false, isLoding);
  271. }
  272. /**GET请求 */
  273. const getRequest = (url, data, success, isLoad) => {
  274. header['content-type'] = 'application/json;charset=UTF-8'
  275. baseRequest(url, data, 'get', success, isLoad);
  276. }
  277. /**Put请求 */
  278. const putRequest = (url, data, success, isLoad) => {
  279. header['content-type'] = 'application/json;charset=UTF-8'
  280. baseRequest(url, data, 'put', success, isLoad)
  281. }
  282. const putRequestJson = (url, data, success, isLoad) => {
  283. header['content-type'] = 'application/json;charset=UTF-8'
  284. baseRequest(url, data, 'put', success, isLoad)
  285. }
  286. /**
  287. * 上传文件
  288. * @param {*} url 请求url
  289. * @param {*} data 携带数据
  290. * @param {*} success 请求成功函数
  291. */
  292. const uploadFile = (url, data, success) => {
  293. // console.log(env[env.NODE_ENV].apiUrl + url, data)
  294. getToken(token => {
  295. uni.uploadFile({
  296. url: env[env.NODE_ENV].apiUrl + url,
  297. filePath: data,
  298. name: 'file',
  299. header: {
  300. 'appId': header['appId'],
  301. 'authorityToken': token
  302. },
  303. success(res) {
  304. if (res.statusCode !== 200) return msg('文件上传失败');
  305. let data = JSON.parse(res.data);
  306. if (data.code !== 0) return msg(data.msg);
  307. success.call(this, data.data);
  308. },
  309. })
  310. })
  311. }
  312. /**弹窗 */
  313. const msg = (title, success) => {
  314. if (title) {
  315. uni.showToast({
  316. title: title,
  317. icon: 'none',
  318. success() {
  319. if (success) success.call(this);
  320. }
  321. });
  322. }
  323. return false;
  324. }
  325. const msgConfirm = (msg, success, cancel) => {
  326. uni.showModal({
  327. title: '提示',
  328. content: msg,
  329. success(res) {
  330. if (res.confirm) {
  331. success.call(this);
  332. } else {
  333. if (cancel) cancel.call(this);
  334. }
  335. }
  336. })
  337. }
  338. const msgConfirmText = (msg, confirmText, success, cancel) => {
  339. uni.showModal({
  340. title: '提示',
  341. content: msg,
  342. confirmText: confirmText,
  343. success(res) {
  344. if (res.confirm) {
  345. success.call(this);
  346. } else {
  347. if (cancel) cancel.call(this);
  348. }
  349. }
  350. })
  351. }
  352. const alertMsg = (msg, success) => {
  353. uni.showModal({
  354. title: '提示',
  355. content: msg,
  356. showCancel: false,
  357. success(res) {
  358. if (success) success.call(this);
  359. }
  360. })
  361. return false;
  362. }
  363. const load = msg => {
  364. uni.showLoading({
  365. title: msg,
  366. mask: true
  367. })
  368. }
  369. const g = (url, success, isLoad) => {
  370. getRequest(url, {}, success, isLoad)
  371. }
  372. const p = (url, success, isLoad) => {
  373. postRequest(url, {}, success, isLoad)
  374. }
  375. const send = (url, mobile, success, error) => {
  376. if (!mobile) {
  377. return msg('手机号码不能为空', error);
  378. }
  379. postRequest(url, {
  380. phone: mobile
  381. }, json => {
  382. if (json.code !== 0) {
  383. return msg(json.msg, error);
  384. }
  385. let time = 60;
  386. const initTime = setInterval(() => {
  387. time--;
  388. if (time > 0) {
  389. success.call(this, time + '秒后获取');
  390. } else {
  391. clearInterval(initTime);
  392. success.call(this, '获取验证码');
  393. }
  394. }, 1000)
  395. })
  396. }
  397. // 登陆拦截
  398. const redirectIndex = (params) => {
  399. // uni.clearStorageSync();
  400. // console.log(params)
  401. var pages = getCurrentPages();
  402. // console.log("pages》》》》》登陆拦截", pages);
  403. let currentUrl;
  404. if (pages) {
  405. var currentPage = pages[pages.length - 1];
  406. if (currentPage) {
  407. currentUrl = currentPage.route;
  408. let query = currentPage.data.query;
  409. if (query) {
  410. for (let key in query) {
  411. const fo = key + '=' + query[key];
  412. currentUrl += currentUrl.indexOf('?') > -1 ? '&' + fo : '?' + fo;
  413. }
  414. }
  415. if (currentUrl) {
  416. console.log('currentUrl》》》》》', currentUrl);
  417. setStorage("REDIRECT_URL", '/' + currentUrl)
  418. if (currentUrl == 'pages/authorize/authorize') {
  419. console.log('当前已经处于登录页面,不在向下执行跳转');
  420. return
  421. }
  422. }
  423. }
  424. }
  425. let redirectUrl = '/pages/authorize/authorize' + (params ? params : '');
  426. // console.log("登陆拦截")
  427. uni.navigateTo({
  428. url: redirectUrl
  429. })
  430. return false;
  431. }
  432. const saveImage = (url) => {
  433. uni.saveImageToPhotosAlbum({
  434. filePath: url,
  435. success() {
  436. msg('图片保存成功');
  437. }
  438. })
  439. }
  440. const saveImageToPhotosAlbum = (url) => {
  441. if (!url) return msg('小程序码不存在');
  442. uni.getImageInfo({
  443. src: url,
  444. success(json) {
  445. // uni.getSetting({
  446. // success(res) {
  447. // if (!res.authSetting['scope.writePhotosAlbum']) {
  448. // uni.authorize({
  449. // scope: 'scope.writePhotosAlbum',
  450. // success() {
  451. // saveImage(json.path);
  452. // }
  453. // })
  454. // } else {
  455. // saveImage(json.path);
  456. // }
  457. // }
  458. // })
  459. }
  460. })
  461. }
  462. const getConfig = (config, id) => {
  463. let con = {};
  464. config.info.forEach(inf => {
  465. // console.log(inf.version, inf.version ? inf.version.indexOf(id) : '-')
  466. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  467. con[inf.setName] = inf.setDefault;
  468. });
  469. config.card.forEach(inf => {
  470. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  471. con[inf.setName] = inf.setDefault;
  472. });
  473. config.realTime.forEach(inf => {
  474. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  475. con[inf.setName] = inf.setDefault;
  476. });
  477. return con;
  478. }
  479. const isLogin = async (error) => {
  480. let userInfo = getStorage('userInfo');
  481. if (!userInfo || !userInfo.id) {
  482. if (error) return false;
  483. // console.log("第一次执行")
  484. return redirectIndex();
  485. }
  486. return true;
  487. }
  488. //检测是否登录
  489. const isLogins = (isGoLogin) => {
  490. let userInfo = getStorage('userInfo');
  491. if (!userInfo) {
  492. if (isGoLogin) {
  493. redirectIndex();
  494. }
  495. return false;
  496. }
  497. return true;
  498. }
  499. const setStorage = (key, value) => {
  500. uni.setStorageSync(env.NODE_ENV + "_" + key, value)
  501. }
  502. const getStorage = (key) => {
  503. return uni.getStorageSync(env.NODE_ENV + "_" + key)
  504. }
  505. const removeStorage = (key) => {
  506. return uni.removeStorageSync(env.NODE_ENV + "_" + key);
  507. }
  508. /**
  509. * 缓存模板id
  510. */
  511. // const gettmplIds = () => {
  512. // getRequest('/api/wxmes/querylist', {}, res => {
  513. // setStorage('tmplIds', res)
  514. // console.log(res)
  515. // })
  516. // }
  517. //微信支付订单公共方法
  518. const payOrder = (id,success) => {
  519. var loctionAddressMap = getStorage('loctionAddressMap');
  520. var datas = {
  521. id: id
  522. };
  523. if (loctionAddressMap) {
  524. datas.province = loctionAddressMap.province;
  525. datas.city = loctionAddressMap.city;
  526. };
  527. if(getStorage('isVideoScene')){
  528. uni.checkBeforeAddOrder({
  529. success (res){
  530. console.log('下单前置检查',res)
  531. let data = res.data;
  532. data.requiredFundType = 0;
  533. datas.sceneCheckStr = JSON.stringify(data);
  534. wexinPay(datas,success)
  535. },
  536. })
  537. }else{
  538. wexinPay(datas,success)
  539. }
  540. }
  541. //支付中或已支付
  542. const payStatus = (json, id, success) =>{
  543. if(json&&json.code === 115){
  544. setTimeout(res=>{
  545. uni.hideLoading();
  546. isShowLoading = false;
  547. uni.showModal({
  548. title: '提示',
  549. content: '当前订单正在支付或30秒后重试',
  550. confirmText: '我知道了',
  551. showCancel: false,
  552. success(res) {
  553. }
  554. })
  555. },3000)
  556. return false;
  557. }
  558. if (json.type === 2 || json.type === 3) {
  559. //调用后台判断订单是否支付成功
  560. let i = 0;
  561. let timer = setInterval(() => {
  562. postRequest('/api/order/check', {
  563. id: id
  564. }, res => {
  565. if (res.isSuccess || i === 4) {
  566. clearInterval(timer);
  567. success.call(this, res.isSuccess);
  568. } else {
  569. i++;
  570. }
  571. }, true)
  572. }, 200);
  573. // success.call(this);
  574. return false;
  575. }
  576. }
  577. // 支付成功回调
  578. const paySuccess = (id,success) =>{
  579. console.log('支付成功')
  580. let i = 0;
  581. let timer = setInterval(() => {
  582. postRequest('/api/order/check', {
  583. id: id
  584. }, res => {
  585. console.log(i)
  586. if (res.isSuccess || i === 5) {
  587. clearInterval(timer);
  588. success.call(this, res.isSuccess);
  589. } else {
  590. i++;
  591. }
  592. }, true)
  593. }, 500);
  594. }
  595. // 支付失败回调
  596. const payFail = (id) =>{
  597. console.log('支付失败')
  598. postRequest('/api/order/cancelPay', {
  599. id: id
  600. }, data => {
  601. uni.redirectTo({
  602. url: '/mine/orderDet/orderDet?id=' + id,
  603. })
  604. })
  605. }
  606. const wexinPay = (datas, success) => {
  607. let isShowLoading = false;
  608. if (!isShowLoading) {
  609. loadIng('加载中');
  610. // console.log('加载中')
  611. isShowLoading = true;
  612. }
  613. // #ifdef MP-WEIXIN
  614. postRequest('/api/order/weixin/pay', datas, json => {
  615. // console.log('json',json)
  616. payStatus(json, datas.id, success)
  617. const sceneCheckStr = datas.sceneCheckStr;
  618. if(sceneCheckStr && JSON.parse(sceneCheckStr).requiredFundType === 1){
  619. console.log('拉起收银台')
  620. wx.requestOrderPayment({
  621. timeStamp: json.timeStamp,
  622. nonceStr: json.nonceStr,
  623. package: json.packages,
  624. signType: json.signType,
  625. orderInfo: json.orderInfo,
  626. paySign: json.sign,
  627. success: function() {
  628. paySuccess(datas.id,success)
  629. },
  630. fail: function(res) {
  631. console.log(res);
  632. payFail(datas.id)
  633. }
  634. })
  635. }else{
  636. uni.requestPayment({
  637. timeStamp: json.timeStamp,
  638. nonceStr: json.nonceStr,
  639. package: json.packages,
  640. signType: json.signType,
  641. paySign: json.sign,
  642. success: function() {
  643. paySuccess(datas.id,success)
  644. },
  645. fail: function(res) {
  646. console.log(res);
  647. payFail(datas.id)
  648. }
  649. })
  650. }
  651. if (isShowLoading) {
  652. uni.hideLoading();
  653. isShowLoading = false;
  654. }
  655. })
  656. // #endif
  657. // #ifdef MP-ALIPAY
  658. datas.payChannel = 'alipay_lite ';
  659. postRequest('/api/order/weixin/pay', datas, json => {
  660. // console.log(json)
  661. if (json.type === 2 || json.type === 3) {
  662. //调用后台判断订单是否支付成功
  663. let i = 0;
  664. let timer = setInterval(() => {
  665. postRequest('/api/order/check', {
  666. id: id
  667. }, res => {
  668. if (res.isSuccess || i === 4) {
  669. clearInterval(timer);
  670. success.call(this, res.isSuccess);
  671. } else {
  672. i++;
  673. }
  674. }, true)
  675. }, 200);
  676. // success.call(this);
  677. return false;
  678. }
  679. uni.requestPayment({
  680. provider: 'alipay',
  681. orderInfo: json.packages,
  682. success: function() {
  683. paySuccess(datas.id,success)
  684. },
  685. fail: function(res) {
  686. payFail(datas.id)
  687. console.log(res);
  688. }
  689. })
  690. if (isShowLoading) {
  691. uni.hideLoading();
  692. isShowLoading = false;
  693. }
  694. })
  695. // #endif
  696. }
  697. //微信支付订单公共方法
  698. const payOrdersss = (id, success) => {
  699. let isShowLoading = false;
  700. if (!isShowLoading) {
  701. loadIng('加载中');
  702. // console.log('加载中')
  703. isShowLoading = true;
  704. }
  705. var loctionAddressMap = getStorage('loctionAddressMap');
  706. var datas = {
  707. id: id
  708. };
  709. if (loctionAddressMap) {
  710. datas.province = loctionAddressMap.province;
  711. datas.city = loctionAddressMap.city;
  712. };
  713. // #ifdef MP-WEIXIN
  714. postRequest('/api/order/weixin/pay', datas, json => {
  715. // console.log(json)
  716. if (json.type === 2 || json.type === 3) {
  717. //调用后台判断订单是否支付成功
  718. let i = 0;
  719. let timer = setInterval(() => {
  720. postRequest('/api/order/check', {
  721. id: id
  722. }, res => {
  723. if (res.isSuccess || i === 4) {
  724. clearInterval(timer);
  725. success.call(this, res.isSuccess);
  726. } else {
  727. i++;
  728. }
  729. }, true)
  730. }, 200);
  731. // success.call(this);
  732. return false;
  733. }
  734. uni.requestPayment({
  735. timeStamp: json.timeStamp,
  736. nonceStr: json.nonceStr,
  737. package: json.packages,
  738. signType: json.signType,
  739. paySign: json.sign,
  740. success: function() {
  741. console.log('支付成功')
  742. let i = 0;
  743. let timer = setInterval(() => {
  744. postRequest('/api/order/check', {
  745. id: id
  746. }, res => {
  747. console.log(i)
  748. if (res.isSuccess || i === 5) {
  749. clearInterval(timer);
  750. success.call(this, res.isSuccess);
  751. } else {
  752. i++;
  753. }
  754. }, true)
  755. }, 500);
  756. },
  757. fail: function(res) {
  758. console.log('支付失败')
  759. postRequest('/api/order/cancelPay', {
  760. id: id
  761. }, data => {
  762. uni.redirectTo({
  763. url: '/mine/orderDet/orderDet?id=' + id,
  764. })
  765. })
  766. console.log(res);
  767. }
  768. })
  769. if (isShowLoading) {
  770. uni.hideLoading();
  771. isShowLoading = false;
  772. }
  773. })
  774. // #endif
  775. // #ifdef MP-ALIPAY
  776. datas.payChannel = 'alipay_lite ';
  777. postRequest('/api/order/weixin/pay', datas, json => {
  778. // console.log(json)
  779. if (json.type === 2 || json.type === 3) {
  780. //调用后台判断订单是否支付成功
  781. let i = 0;
  782. let timer = setInterval(() => {
  783. postRequest('/api/order/check', {
  784. id: id
  785. }, res => {
  786. if (res.isSuccess || i === 4) {
  787. clearInterval(timer);
  788. success.call(this, res.isSuccess);
  789. } else {
  790. i++;
  791. }
  792. }, true)
  793. }, 200);
  794. // success.call(this);
  795. return false;
  796. }
  797. uni.requestPayment({
  798. provider: 'alipay',
  799. orderInfo: json.packages,
  800. success: function() {
  801. console.log('支付成功')
  802. let i = 0;
  803. let timer = setInterval(() => {
  804. postRequest('/api/order/check', {
  805. id: id
  806. }, res => {
  807. console.log(i)
  808. if (res.isSuccess || i === 5) {
  809. clearInterval(timer);
  810. success.call(this, res.isSuccess);
  811. } else {
  812. i++;
  813. }
  814. }, true)
  815. }, 500);
  816. },
  817. fail: function(res) {
  818. console.log('支付失败')
  819. postRequest('/api/order/cancelPay', {
  820. id: id
  821. }, data => {
  822. uni.redirectTo({
  823. url: '/mine/orderDet/orderDet?id=' + id,
  824. })
  825. })
  826. console.log(res);
  827. }
  828. })
  829. if (isShowLoading) {
  830. uni.hideLoading();
  831. isShowLoading = false;
  832. }
  833. })
  834. // #endif
  835. }
  836. //微信支付订单公共方法 ==活动
  837. const payOrders = (id, success, type) => {
  838. let isShowLoading = false;
  839. if (!isShowLoading) {
  840. loadIng('加载中');
  841. // console.log('加载中')
  842. isShowLoading = true;
  843. }
  844. postRequest('/api/order/weixin/pay', {
  845. id: id
  846. }, json => {
  847. // console.log(json)
  848. payStatus(json, id, success)
  849. uni.requestPayment({
  850. timeStamp: json.timeStamp,
  851. nonceStr: json.nonceStr,
  852. package: json.packages,
  853. signType: json.signType,
  854. paySign: json.sign,
  855. success: function() {
  856. paySuccess(id, success)
  857. },
  858. fail: function(res) {
  859. console.log('支付失败')
  860. postRequest('/api/order/cancelPay', {
  861. id: id
  862. }, data => {
  863. uni.redirectTo({
  864. url: '/mine/activity/activity',
  865. })
  866. })
  867. console.log(res);
  868. }
  869. })
  870. if (isShowLoading) {
  871. uni.hideLoading();
  872. isShowLoading = false;
  873. }
  874. })
  875. }
  876. const authSetting = (authority, success, error) => {
  877. // console.log(authority,success)
  878. // #ifndef H5
  879. uni.getSetting({
  880. success(res) {
  881. if (res.authSetting[authority]) {
  882. success.call(this);
  883. return false;
  884. }
  885. uni.authorize({
  886. scope: authority,
  887. success() {
  888. success.call(this);
  889. },
  890. fail: function(res) {
  891. error.call(this);
  892. }
  893. })
  894. }
  895. })
  896. // #endif
  897. }
  898. const getLocation = (suss) => {
  899. // #ifdef MP-WEIXIN
  900. authSetting('scope.userLocation', () => {
  901. // load('定位中…');
  902. uni.getLocation({
  903. type: 'gcj02',
  904. // isHighAccuracy: true,
  905. success: function(res) {
  906. // uni.hideLoading();
  907. console.log(res, "789")
  908. suss.call(this, res);
  909. },
  910. fail: function(res) {
  911. console.log("调用失败", res)
  912. suss.call(this, 2)
  913. // uni.hideLoading();
  914. }
  915. })
  916. }, () => {
  917. // uni.hideLoading();
  918. console.log("取消")
  919. suss.call(this, 1)
  920. });
  921. // #endif
  922. // #ifdef MP-ALIPAY
  923. uni.getLocation({
  924. type: 'gcj02',
  925. // isHighAccuracy: true,
  926. success: function(res) {
  927. // uni.hideLoading();
  928. console.log(res, "789")
  929. suss.call(this, res);
  930. },
  931. fail: function(res) {
  932. console.log("调用失败", res)
  933. suss.call(this, 2)
  934. // uni.hideLoading();
  935. }
  936. })
  937. // #endif
  938. // #ifdef H5
  939. uni.getLocation({
  940. type: 'gcj02',
  941. isHighAccuracy: true,
  942. success: function(res) {
  943. // uni.hideLoading();
  944. console.log(res)
  945. suss.call(this, res, "789");
  946. },
  947. fail: function() {
  948. console.log("调用失败", res)
  949. suss.call(this, 2)
  950. // uni.hideLoading();
  951. }
  952. })
  953. // #endif
  954. }
  955. const scopeAddress = (success) => {
  956. // #ifdef MP-WEIXIN
  957. authSetting('scope.address', () => {
  958. uni.chooseAddress({
  959. success: function(res) {
  960. success.call(this, res);
  961. },
  962. })
  963. }, () => {
  964. msg('未设置开放权限')
  965. });
  966. // #endif
  967. // #ifdef MP-ALIPAY
  968. uni.chooseAddress({
  969. success: function(res) {
  970. success.call(this, res);
  971. },
  972. })
  973. // #endif
  974. }
  975. const isAuth = () => {
  976. const user = getStorage('userInfo');
  977. return user && user.id;
  978. }
  979. // 去掉字符串中的特殊字符和转义字符
  980. const excludeSpecial = (s) => {
  981. // 去掉转义字符
  982. // s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
  983. const pattern = /[`~!@#$^&*()=|{}':;',\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""' + - - _ % 。,、?\s]/g;
  984. if (s != undefined || s != null) {
  985. s = s.replace(pattern, "")
  986. }
  987. return s;
  988. }
  989. // 行为操作
  990. const saveBehavior = (params, success) => {
  991. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、海报
  992. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知 13、素材 15、科室 16、海报 20、医生 21日历、探索 47评价
  993. postRequest('/api/v3/save', params, data => {
  994. success.call(this, data)
  995. })
  996. }
  997. const getBehavior = (params, success) => {
  998. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、海报
  999. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知 13、素材 15、科室 16、海报 20、医生 21日历、探索 47评价
  1000. console.log('行为')
  1001. getRequest('/api/v3/get', params, data => {
  1002. success.call(this, data)
  1003. })
  1004. }
  1005. /**
  1006. * text:展示导航名
  1007. * iconPath:未点击显示图标
  1008. * selectedIconPath:选中图标
  1009. * pagePath:页面路径
  1010. * channel:匹配名
  1011. * click:点击事件
  1012. * index:排序
  1013. * show:是否显示
  1014. */
  1015. var selectedIconPath
  1016. if (header.appId == 'ZQ1VK5oc17I387E') {
  1017. selectedIconPath = '/static/pages/images/' + publics.iconSrc + 'yunyao_h.png'
  1018. } else {
  1019. selectedIconPath = '/static/pages/images/' + publics.iconSrc + 'home_h.png'
  1020. }
  1021. var tab = [{
  1022. text: "首页",
  1023. iconPath: '/static/pages/images/self/index.png',
  1024. selectedIconPath: '/static/pages/images/self/index_h.png',
  1025. pagePath: 'pages/index/index',
  1026. channel: 'home',
  1027. click: 'goHome',
  1028. index: 1,
  1029. show: true
  1030. },
  1031. {
  1032. text: "机柜",
  1033. iconPath: '/static/pages/images/self/machine.png',
  1034. selectedIconPath: '/static/pages/images/self/machine_h.png',
  1035. pagePath: 'pages/machine/machine',
  1036. click: 'goMachine',
  1037. channel: 'machine',
  1038. index: 2,
  1039. show: true
  1040. },
  1041. {
  1042. text: "电池",
  1043. iconPath: '/static/pages/images/self/cell.png',
  1044. selectedIconPath: '/static/pages/images/self/cell_h.png',
  1045. pagePath: 'pages/cell/cell',
  1046. click: 'goCell',
  1047. channel: 'cell',
  1048. index: 3,
  1049. show: true
  1050. },
  1051. {
  1052. text: "订单",
  1053. iconPath: '/static/pages/images/self/order.png',
  1054. selectedIconPath: '/static/pages/images/self/order_h.png',
  1055. pagePath: 'pages/order/order',
  1056. click: 'goOrder',
  1057. channel: 'order',
  1058. index: 5,
  1059. show: true
  1060. },
  1061. {
  1062. text: "我的",
  1063. iconPath: '/static/pages/images/self/user.png',
  1064. selectedIconPath: '/static/pages/images/self/user_h.png',
  1065. pagePath: 'pages/user/user',
  1066. click: 'goUser',
  1067. channel: 'user',
  1068. index: 6,
  1069. show: true
  1070. }
  1071. ]
  1072. module.exports = {
  1073. setStorage: setStorage,
  1074. getStorage: getStorage,
  1075. postRequest: postRequest,
  1076. postRequestLoding: postRequestLoding,
  1077. getRequest: getRequest,
  1078. putRequest: putRequest,
  1079. putRequestJson: putRequestJson,
  1080. msg: msg,
  1081. g: g,
  1082. p: p,
  1083. env: env,
  1084. send: send,
  1085. redirectIndex: redirectIndex,
  1086. saveImage: saveImage,
  1087. saveImageToPhotosAlbum: saveImageToPhotosAlbum,
  1088. uploadFile: uploadFile,
  1089. msgConfirm: msgConfirm,
  1090. getConfig: getConfig,
  1091. load: load,
  1092. isLogin: isLogin,
  1093. isLogins: isLogins,
  1094. payOrder: payOrder,
  1095. payOrders: payOrders,
  1096. getLocation: getLocation,
  1097. scopeAddress: scopeAddress,
  1098. isAuth: isAuth,
  1099. alertMsg: alertMsg,
  1100. getToken: getToken,
  1101. loadIng: loadIng,
  1102. removeStorage: removeStorage,
  1103. public: publics,
  1104. header: header,
  1105. // gettmplIds: gettmplIds,
  1106. excludeSpecial: excludeSpecial,
  1107. tab: tab,
  1108. msgConfirmText: msgConfirmText,
  1109. silenceLogin: silenceLogin,
  1110. getWXUserProfile: getWXUserProfile,
  1111. updataWxInfos: updataWxInfos,
  1112. saveBehavior: saveBehavior,
  1113. getBehavior: getBehavior
  1114. }