request.js 26 KB

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