request.js 27 KB

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