BaseForm.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\third\wechat;
  8. use app\models\Option;
  9. use app\models\StoreMini;
  10. use app\modules\admin\models\WechatThirdErrorMsgForm as ErrorMsg;
  11. use app\utils\CurlHelper;
  12. use EasyWeChat\Factory;
  13. use yii\base\Model;
  14. class BaseForm extends Model
  15. {
  16. /**
  17. * @var \EasyWeChat\OpenPlatform\Application
  18. */
  19. public $openPlatform;
  20. //商城ID
  21. public $store_id;
  22. //小程序ID
  23. public $mini_id;
  24. //设置规则
  25. public function rules()
  26. {
  27. return [
  28. [['store_id', 'mini_id'], 'integer']
  29. ];
  30. }
  31. public function __construct ($config = [])
  32. {
  33. parent::__construct($config);
  34. //微信三方配置信息
  35. $wx_config = [
  36. //微信三方appid
  37. 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
  38. //微信三方appSecret
  39. 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
  40. //微信三方平台设置的token
  41. 'token' => Option::get("platform_token", 0, 'saas')['value'],
  42. //微信三方平台设置的加密key
  43. 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
  44. ];
  45. $this->openPlatform = Factory::openPlatform($wx_config);
  46. }
  47. /**
  48. * 微信业务实现
  49. */
  50. public function miniProgram ($appid = "")
  51. {
  52. try {
  53. $store_id = $this->store_id;
  54. //获取小程序信息
  55. $store_mini = StoreMini::find()->where(
  56. ['or',
  57. ['id' => $this->mini_id],
  58. ['appid' => $appid, 'store_id' => $store_id],
  59. ['store_id' => $store_id]
  60. ])->orderBy('id desc')->one();
  61. if (empty($store_mini->appid) || empty($store_mini->authorizer_refresh_token)) {
  62. throw new \Exception("获取小程序信息失败");
  63. }
  64. //配置easyWechat
  65. return $this->openPlatform->miniProgram($store_mini->appid, $store_mini->authorizer_refresh_token);
  66. } catch (\Exception $e) {
  67. return [
  68. 'code' => 1,
  69. 'msg' => $e->getMessage()
  70. ];
  71. }
  72. }
  73. //获取网络图片到临时目录
  74. public function saveTempImage($url)
  75. {
  76. if (strpos($url, 'http') === false) {
  77. $url = 'http:' . trim($url);
  78. }
  79. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  80. mkdir(\Yii::$app->runtimePath . '/image');
  81. }
  82. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  83. CurlHelper::download($url, $save_path);
  84. return $save_path;
  85. }
  86. //递归
  87. public function getdata($data, $id = [0])
  88. {
  89. foreach ($data as $k => $v) {
  90. if (in_array($v['id'], $id)) {
  91. $v['children'] = $this->getdata($data, $v['children']);
  92. $arr[] = $v;
  93. }
  94. }
  95. return $arr;
  96. }
  97. //返回中文错误信息
  98. public function getZnMsg($result)
  99. {
  100. $ErrorMsg = new ErrorMsg();
  101. $arr = $ErrorMsg->getArray();
  102. $msg = !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg'];
  103. return $msg;
  104. }
  105. }