sdkUtil.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { config } from "../config/config"
  2. import { wx_pay_data } from "../data/data";
  3. import { log } from "./log"
  4. import { tools } from "./tools"
  5. // export default {
  6. // onShareAppMessage() {
  7. // return {
  8. // title: '分享给微信好友',
  9. // path: '/pages/bookstore/bookstore'
  10. // };
  11. // }
  12. // }
  13. export class sdkUtil {
  14. public static login(cb:Function) {
  15. switch (tools.getCurPlatform()){
  16. case config.Platform.H5:
  17. cb && cb(null)
  18. break
  19. case config.Platform.WEIXIN:
  20. wx.login({
  21. success(res) {
  22. let code = res.code
  23. log.Debug("wx_code",code)
  24. cb && cb({'code':code})
  25. },
  26. fail(e) { log.Debug(`wx_login 调用失败:`,e) }
  27. })
  28. break;
  29. case config.Platform.TOUTIAO:
  30. tt.login({
  31. force: true,
  32. success(res) {
  33. let code = res.code
  34. let anonymousCode = res.anonymousCode
  35. log.Debug("tt_code",code,'tt_anonymousCode',anonymousCode)
  36. cb && cb({'code':code,'anonymousCode':anonymousCode})
  37. },
  38. fail(e) { log.Debug(`tt_login 调用失败:`,e) }
  39. });
  40. break;
  41. default:
  42. break;
  43. }
  44. }
  45. public static showPayment(data:wx_pay_data,cb:Function){
  46. switch (tools.getCurPlatform()){
  47. case config.Platform.WEIXIN:
  48. wx.requestPayment({
  49. timeStamp: data.timeStamp,
  50. nonceStr: data.nonceStr,
  51. package: data.package,
  52. paySign: data.paySign,
  53. signType:"RSA",
  54. success: (res) => {
  55. console.log("showPayment - success",res)
  56. cb && cb(null,res)
  57. },
  58. fail: (res) => {
  59. console.log("showPayment - fail",res)
  60. cb && cb("fail!",res)
  61. },
  62. complete: (res) => {
  63. console.log("showPayment - complete",res,data)
  64. // cb && cb(res)
  65. }
  66. });
  67. break;
  68. case config.Platform.TOUTIAO:
  69. break;
  70. default:
  71. cb && cb()
  72. break;
  73. }
  74. }
  75. /**
  76. * 添加桌面
  77. */
  78. public static addShortcut(cb:Function) {
  79. switch (tools.getCurPlatform()){
  80. case config.Platform.WEIXIN:
  81. wx.addShortcut({
  82. success() {
  83. console.log("添加桌面成功");
  84. cb && cb()
  85. },
  86. fail(err) {
  87. console.log("添加桌面失败", err.errMsg);
  88. },
  89. });
  90. break;
  91. case config.Platform.TOUTIAO:
  92. tt.addShortcut({
  93. success() {
  94. console.log("添加桌面成功");
  95. cb && cb()
  96. },
  97. fail(err) {
  98. console.log("添加桌面失败", err.errMsg);
  99. },
  100. });
  101. break;
  102. default:
  103. cb && cb()
  104. break;
  105. }
  106. }
  107. }