request.js 29 KB

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