PerfectMiniForm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\third\ali;
  8. use app\models\StoreAliMini;
  9. use app\models\StoreAliMiniVersion;
  10. use app\models\StoreMiniToken;
  11. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniBaseinfoModifyRequest;
  12. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniCategoryQueryRequest;
  13. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIndividualBusinessCertifyRequest;
  14. use app\utils\Image;
  15. use yii\db\StaleObjectException;
  16. class PerfectMiniForm extends BaseForm
  17. {
  18. /**
  19. * 完善信息form
  20. */
  21. public $mini_id;
  22. public $store_id;
  23. public function inits()
  24. {
  25. try {
  26. $mini_id = $this->mini_id;
  27. if (!empty($mini_id)) {
  28. $store_mini = StoreAliMini::findOne($mini_id);
  29. if (empty($store_mini)) {
  30. return [
  31. 'code' => 1,
  32. 'msg' => "查找不到小程序信息"
  33. ];
  34. }
  35. $this->store_mini = $store_mini;
  36. $this->mini_version = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy('id desc')->one();
  37. }
  38. } catch (\Exception $e) {
  39. return [
  40. 'code' => 1,
  41. 'msg' => $e->getMessage()
  42. ];
  43. }
  44. }
  45. //小程序类目查询 alipay.open.mini.category.query(小程序类目树查询)
  46. public function getAllCategory()
  47. {
  48. try {
  49. $result = $this->inits();
  50. if ($result['code'] === 1) {
  51. return $result;
  52. }
  53. //请求参数
  54. $param = json_encode([
  55. //是否过滤为可用分类
  56. "is_filter" => 1
  57. ]);
  58. //请求配置
  59. $request = new AlipayOpenMiniCategoryQueryRequest();
  60. $result = $this->miniCommon($request, $param);
  61. //请求成功
  62. if ($result->code == 10000) {
  63. //处理数据
  64. $arr = json_decode(json_encode($result->mini_category_list), true);
  65. $data = $this->getdata($arr);
  66. //返回结果
  67. return [
  68. 'code' => 0,
  69. 'msg' => "成功",
  70. 'data' => $data,
  71. 'old_data' => $result->mini_category_list
  72. ];
  73. } else {
  74. //请求失败
  75. throw new \Exception($result->sub_msg);
  76. }
  77. } catch (\Exception $e) {
  78. return [
  79. 'code' => 0,
  80. 'msg' => $e->getMessage()
  81. ];
  82. }
  83. }
  84. //小程序修改基础信息 alipay.open.mini.baseinfo.modify(小程序修改基础信息)
  85. public function setMiniBaseInfo()
  86. {
  87. $t = \Yii::$app->db->beginTransaction();
  88. try {
  89. $result = $this->inits();
  90. if ($result['code'] === 1) {
  91. return $result;
  92. }
  93. //小程序Model
  94. $store_mini = $this->store_mini;
  95. //请求信息
  96. $param_info = $this->param_info;
  97. //处理分类数据 变为:一级分类ID_二级分类ID_三级分类ID;一级分类ID_二级分类ID_;
  98. // $category_ids = "";
  99. // //名称
  100. // $category_label = "";
  101. // if (!empty($param_info['mini_categoryIds'])) {
  102. // foreach ($param_info['mini_categoryIds'] as $item) {
  103. // $arr = [];
  104. // $label = [];
  105. // foreach ($item as $child) {
  106. // array_push($arr, $child['value']);
  107. // array_push($label, $child['label']);
  108. // }
  109. // $str = implode('_', $arr);
  110. // $str_label = implode('_', $label);
  111. // $category_ids .= $str . ';';
  112. // $category_label .= $str_label . ';';
  113. // }
  114. // }
  115. //保存数据到数据库
  116. $category_ids = "XS1020_XS2164_XS3169;";
  117. $category_label = "购物_零售电商_综合零售商品;";
  118. //小程序名称
  119. $store_mini->app_name = $param_info['app_name'];
  120. //小程序英文名称
  121. $store_mini->app_english_name = $param_info['app_english_name'];
  122. //小程序简介
  123. $store_mini->app_slogan = $param_info['app_slogan'];
  124. //小程序LOGO
  125. $store_mini->app_logo = $param_info['app_logo'];
  126. //小程序描述
  127. $store_mini->app_desc = $param_info['app_desc'];
  128. //客服手机号
  129. $store_mini->service_phone = $param_info['service_phone'];
  130. //客服邮箱
  131. $store_mini->service_email = $param_info['service_email'];
  132. //分类信息
  133. $store_mini->mini_categoryIds = $category_ids;//;
  134. //提交的分类数据
  135. $store_mini->category_data = json_encode($param_info['categories']);
  136. //分类名称
  137. $store_mini->category_name = $category_label;//$category_label;
  138. //保存数据
  139. $root_path = $this->saveTempImage($param_info['app_logo']);
  140. $image_size = getimagesize($root_path);
  141. $w = $image_size[0];
  142. $h = $image_size[1];
  143. if ($w > $h) {
  144. $w = $h;
  145. }
  146. if ($w < $h) {
  147. $h = $w;
  148. }
  149. $image_result = (new Image())->image_resize($root_path, $root_path, $w, $h);
  150. if (!$image_result) {
  151. throw new \Exception("图片生成失败");
  152. }
  153. if (!$store_mini->save()) {
  154. throw new \Exception(json_encode($store_mini->errors));
  155. }
  156. //调用接口
  157. $request = new AlipayOpenMiniBaseinfoModifyRequest();
  158. $request->setAppName($param_info['app_name']); //小程序应用名称。
  159. $request->setAppEnglishName($param_info['app_english_name']); //小程序应用英文名称。
  160. $request->setAppSlogan($param_info['app_slogan']); //小程序应用简介
  161. $request->setAppLogo("@" . $root_path); //小程序应用logo图标,图片格式必须为:png、jpeg、jpg,建议上传像素为180*180。
  162. $request->setAppDesc($param_info['app_desc']); //小程序应用描述,20-200个字
  163. $request->setServicePhone($param_info['service_phone']); //小程序客服电话。
  164. $request->setServiceEmail($param_info['service_email']); //小程序客服邮箱。
  165. $request->setMiniCategoryIds($category_ids); //新小程序前台类目。格式为 第一个一级类目_第一个二级类目;第二个一级类目_第二个二级类目_第二个三级类目。详细类目可以通过
  166. $result = $this->miniCommon($request);
  167. //调用成功
  168. if ($result->code == 10000) {
  169. if ((int)$store_mini->is_merchant === 0 && (empty($param_info['license_pic'] ?: $store_mini->license_pic) || empty($param_info['license_no'] ?: $store_mini->license_no))) {
  170. throw new \Exception("请完善营业执照照片以及编码");
  171. }
  172. // $res = $this->updateBussiness(($param_info['license_pic'] ?: $store_mini->license_pic), ($param_info['license_no'] ?: $store_mini->license_no));
  173. // if ($res['code'] !== 0) {
  174. // $file_name = \Yii::$app->runtimePath . '/logs/updateBussiness.log';
  175. // file_put_contents($file_name, "\r\n" . '[升级个体工商户失败][' . date('Y-m-d H:i:s') . ']' . json_encode($res), FILE_APPEND);
  176. // }
  177. $t->commit();
  178. return [
  179. 'code' => 0,
  180. 'msg' => "成功",
  181. 'data' => $result
  182. ];
  183. } else {
  184. //调用失败
  185. throw new \Exception($result->sub_msg);
  186. }
  187. } catch (\Exception $e) {
  188. $t->rollBack();
  189. return [
  190. 'code' => 1,
  191. 'msg' => $e->getMessage()
  192. ];
  193. }
  194. }
  195. public function updateBussiness($license_pic, $license_no)
  196. {
  197. $t = \Yii::$app->db->beginTransaction();
  198. try {
  199. $result = $this->inits();
  200. $store_id = $this->store_id;
  201. if ($result['code'] === 1) {
  202. return $result;
  203. }
  204. //小程序Model
  205. $store_mini = $this->store_mini;
  206. $url = $this->saveTempImage($license_pic);
  207. $license_pic = $this->image2Base64($url);
  208. if ($license_pic == false) {
  209. throw new \Exception("图片转Base64失败");
  210. }
  211. $token_log = StoreMiniToken::findOne(['store_id' => $store_id]);
  212. if (!empty($token_log->app_auth_token)) {
  213. $token = $token_log->app_auth_token;
  214. } else {
  215. try {
  216. $res = $token_log->delete();
  217. $t->commit();
  218. return [
  219. 'code' => 2,
  220. 'msg' => "账户未认证" . $res
  221. ];
  222. } catch (\Exception $e) {
  223. $t->rollBack();
  224. return [
  225. 'code' => 1,
  226. 'msg' => "参数错误"
  227. ];
  228. }
  229. }
  230. $request = new AlipayOpenMiniIndividualBusinessCertifyRequest();
  231. $request->setLicenseNo($license_no);
  232. $request->setLicensePic($license_pic);
  233. $result = $this->miniCommon($request, null, 0, $token);
  234. if (!empty($result->code) && $result->code == 10000) {
  235. $store_mini->is_merchant = $result->certify_result ? 1 : 0;
  236. if (!$store_mini->save()) {
  237. throw new \Exception($store_mini->errors);
  238. }
  239. $t->commit();
  240. return [
  241. 'code' => 0,
  242. 'msg' => "操作成功"
  243. ];
  244. } else {
  245. throw new \Exception($result->sub_msg);
  246. }
  247. } catch (\Exception $e) {
  248. $t->rollBack();
  249. return [
  250. 'code' => 1,
  251. 'msg' => $e->getMessage()
  252. ];
  253. }
  254. }
  255. }