WechatCardServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\wechat;
  12. use app\dao\wechat\WechatCardDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\DownloadImageService;
  16. use crmeb\services\FormBuilder;
  17. use crmeb\services\SystemConfigService;
  18. use crmeb\services\wechat\OfficialAccount;
  19. use think\facade\Log;
  20. /**
  21. * 微信卡券
  22. * Class WechatCardServices
  23. * @package app\services\wechat
  24. * @mixin WechatCardDao
  25. */
  26. class WechatCardServices extends BaseServices
  27. {
  28. protected $builder;
  29. protected $code_type = [
  30. 'CODE_TYPE_NONE' => '文本',
  31. 'CODE_TYPE_BARCODE' => '一维码',
  32. 'CODE_TYPE_QRCODE' => '二维码',
  33. 'CODE_TYPE_ONLY_QRCODE' => '仅显示二维码',
  34. 'CODE_TYPE_ONLY_BARCODE' => '仅显示一维码',
  35. 'CODE_TYPE_NONE' => '不显示任何码型'
  36. ];
  37. /**
  38. * 构造方法
  39. * WechatCardServices constructor.
  40. * @param WechatCardDao $dao
  41. */
  42. public function __construct(WechatCardDao $dao, FormBuilder $formBuilder)
  43. {
  44. $this->dao = $dao;
  45. $this->builder = $formBuilder;
  46. }
  47. public function getList(array $where)
  48. {
  49. [$page, $limit] = $this->getPageValue();
  50. $count = $this->dao->count($where);
  51. $list = $this->dao->getList($where, $page, $limit);
  52. return compact('count', $list);
  53. }
  54. /**
  55. * 获取微信会员卡信息
  56. * @return array
  57. */
  58. public function getInfo()
  59. {
  60. if (!sys_config('wechat_appid') || !sys_config('wechat_appsecret')) {
  61. throw new AdminException('请先配置公众号的appid、appSecret等参数,且需要开通微信卡券功能');
  62. }
  63. $data = [];
  64. try {
  65. $data['color'] = OfficialAccount::getCardColors()['colors'] ?? [];
  66. } catch (\Throwable $e) {
  67. $data['color'] = [];
  68. }
  69. if (!$data['color']) {
  70. throw new AdminException('请先在公众号后台开通微信卡券功能');
  71. }
  72. $info = $this->dao->getOne(['card_type' => 'member_card', 'status' => 1, 'is_del' => 0]);
  73. $data['info'] = ['custom_cell' => []];
  74. $data['selet'] = 0;
  75. if ($info) {
  76. $info = $info->toArray();
  77. $info['custom_cell'] = $info['especial']['custom_cell'] ?? [];
  78. $data['info'] = $info;
  79. $data['selet'] = $info['background_pic_url'] ? 1 : 0;
  80. }
  81. return $data;
  82. }
  83. public function createForm($formData)
  84. {
  85. $f[] = $this->builder->input('brand_name', '商户名称', $formData['brand_name'] ?? '')->required('请填写名称');
  86. $f[] = $this->builder->input('title', '卡券名称', $formData['title'] ?? '')->required('请填写卡券名称');
  87. $f[] = $this->builder->input('service_phone', '电话', $formData['service_phone'] ?? '')->required('请填写电话');
  88. $f[] = $this->builder->select('code_type', 'CODE展示类型', $formData['code_type'])->setOptions(FormBuilder::setOptions($this->code_type))->multiple(true)->required('请选择CODE展示类型');
  89. $f[] = $this->builder->input('color', '卡券颜色', $formData['color'] ?? '')->required('请填写卡券颜色');
  90. $f[] = $this->builder->frameImage('logo_url', 'LOGO', $this->url(config('admin.admin_prefix') . '/widget.images/index', ['fodder' => 'logo_url'], true), $formData['logo_url'] ?? '')->info('建议300*300')->icon('ios-add')->width('960px')->height('550px')->modal(['footer-hide' => true]);
  91. $f[] = $this->builder->frameImage('background_pic_url', '背景图', $this->url(config('admin.admin_prefix') . '/widget.images/index', ['fodder' => 'background_pic_url'], true), $formData['background_pic_url'] ?? '')->info('建议1000*600')->icon('ios-add')->width('960px')->height('550px')->modal(['footer-hide' => true]);
  92. $f[] = $this->builder->input('notice', '提示', $formData['notice'] ?? '')->required('请填写提示信息');
  93. $f[] = $this->builder->input('description', '描述', $formData['description'] ?? '')->required('请填写描述');
  94. $f[] = $this->builder->textarea('prerogative', '卡券特权说明', $formData['notice'] ?? '')->info('会员卡特权说明,限制1024汉字')->required('请填写卡券特权说明');
  95. return create_form('微信卡券添加', $f, $this->url('/wechat/card/0'), 'post');
  96. }
  97. /**
  98. * 添加|编辑微信卡券
  99. * @param int $id
  100. * @param array $data
  101. * @return bool
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function save(array $data)
  107. {
  108. $card = $this->dao->getOne(['card_type' => 'member_card', 'status' => 1, 'is_del' => 0]);
  109. if ($card) {
  110. [$cardInfo, $especial] = $this->wxCreate($data, $card['card_id']);
  111. unset($data['custom_cell']);
  112. $data['especial'] = $especial;
  113. if (!$this->dao->update($card['id'], $data)) {
  114. throw new AdminException('修改失败');
  115. }
  116. } else {
  117. [$cardInfo, $especial] = $this->wxCreate($data);
  118. $this->activateUserForm($cardInfo['card_id']);
  119. unset($data['custom_cell']);
  120. $data['card_id'] = $cardInfo['card_id'];
  121. $data['especial'] = $especial;
  122. if (!$this->dao->save($data)) {
  123. throw new AdminException('添加失败');
  124. }
  125. }
  126. return true;
  127. }
  128. /**
  129. * 微信卡券添加|编辑
  130. * @param array $card
  131. * @param string $card_id
  132. * @param string $card_type
  133. * @return array
  134. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  135. */
  136. public function wxCreate(array $card, string $card_id = '', string $card_type = 'member_card')
  137. {
  138. if (!sys_config('wechat_appid') || !sys_config('wechat_appsecret')) {
  139. throw new AdminException('请先配置公众号的appid、appSecret等参数,且需要开通微信卡券功能');
  140. }
  141. $baseUrl = sys_config('site_url');
  142. try {
  143. $logo = root_path() . 'public' . app()->make(DownloadImageService::class)->downloadImage($card['logo_url'])['path'];
  144. $background = $card['background_pic_url'] ? (root_path() . 'public' . app()->make(DownloadImageService::class)->downloadImage($card['background_pic_url'])['path']) : '';
  145. } catch (\Throwable $e) {
  146. Log::error('添加会员卡券封面图出错误,原因:' . $e->getMessage());
  147. $logo = root_path() . 'public' . $card['logo_url'];
  148. $background = root_path() . 'public' . $card['background_pic_url'];
  149. }
  150. $base_info = [
  151. 'logo_url' => OfficialAccount::temporaryUpload($logo)->media_id,
  152. 'brand_name' => $card['brand_name'],
  153. 'code_type' => 'CODE_TYPE_NONE',
  154. 'title' => $card['title'],
  155. 'color' => $card['color'] ?: 'Color010',
  156. 'notice' => $card['notice'],
  157. 'service_phone' => $card['service_phone'],
  158. 'description' => $card['description'],
  159. 'use_custom_code' => false,
  160. 'can_share' => true,
  161. 'can_give_friend' => false,
  162. 'get_limit' => 1,
  163. 'date_info' => ['type' => 'DATE_TYPE_PERMANENT'],
  164. 'sku' => ['quantity' => 50000000],
  165. 'promotion_url' => $baseUrl . '/pages/users/user_spread_user/index',
  166. 'promotion_url_name' => '推荐给朋友',
  167. 'promotion_url_sub_title' => ''
  168. ];
  169. if ($card['center_title']) {
  170. $base_info = array_merge($base_info, [
  171. 'center_title' => $card['center_title'],
  172. 'center_sub_title ' => $card['center_sub_title'],
  173. 'center_url' => $card['center_url'],
  174. ]);
  175. }
  176. $integral = SystemConfigService::more(['integral_max_num', 'integral_ratio', 'order_give_integral']);
  177. $especial = [
  178. // 'name' => $card['title'],//入口名称
  179. // 'tips' => $card['description'],//入口提示语
  180. // 'url' => $baseUrl,//入口跳转地址
  181. 'supply_bonus' => true,//显示积分
  182. 'bonus_rule' => [
  183. 'cost_money_unit' => 100,//消费金额。以分为单位
  184. 'increase_bonus' => $integral['order_give_integral'],//对应增加的积分
  185. 'max_increase_bonus' => $integral['integral_max_num'],//用户单次可获取的积分上限
  186. 'cost_bonus_unit' => 1,//每使用X积分
  187. 'reduce_money' => bcmul($integral['integral_ratio'], '100', 0),//抵扣xx元,(这里以分为单位)
  188. ],
  189. 'supply_balance' => false,//是否支持充值
  190. // 'activate_url' => $baseUrl,
  191. 'bonus_url' => $baseUrl . '/pages/users/user_integral/index',//积分详情跳转链接
  192. 'prerogative' => $card['prerogative'],
  193. // 'auto_activate' => false,
  194. 'wx_activate' => true,
  195. 'background_pic_url' => $background ? OfficialAccount::temporaryUpload($background)->media_id : '',
  196. 'custom_field2' => [
  197. 'name_type' => 'FIELD_NAME_TYPE_TIMS',//消费次数
  198. 'url' => $baseUrl . '/pages/users/order_list/index'
  199. ],
  200. 'custom_field3' => [
  201. 'name_type' => 'FIELD_NAME_TYPE_COUPON',//优惠券
  202. 'url' => $baseUrl . '/pages/users/user_coupon/index'
  203. ],
  204. ];
  205. if ($card['custom_cell']) {
  206. $cell_data = [];
  207. $i = 1;
  208. foreach ($card['custom_cell'] as $item) {
  209. $cell_data['custom_cell' . $i] = $item;
  210. $i++;
  211. }
  212. $especial = array_merge($especial, $cell_data);
  213. }
  214. @unlink($logo);
  215. @unlink($background);
  216. if ($card_id) {
  217. unset($especial['bonus_rule']);
  218. unset($base_info['brand_name'], $base_info['sku'], $base_info['use_custom_code']);
  219. return [OfficialAccount::updateCard($card_id, $card_type, $base_info, $especial), array_merge($especial, ['custom_cell' => $card['custom_cell']])];
  220. } else {
  221. return [OfficialAccount::createCard($card_type, $base_info, $especial), array_merge($especial, ['custom_cell' => $card['custom_cell']])];
  222. }
  223. }
  224. /**
  225. * 创建会员卡激活表单
  226. * @param string $cardId
  227. * @return \EasyWeChat\Support\Collection
  228. */
  229. public function activateUserForm(string $cardId)
  230. {
  231. $requireForm = [
  232. 'required_form' => [
  233. 'can_modify' => false,
  234. 'common_field_id_list' => [
  235. 'USER_FORM_INFO_FLAG_MOBILE'
  236. ]
  237. ]
  238. ];
  239. $optionFrom = [
  240. 'optional_form' => [
  241. 'can_modify' => false,
  242. 'common_field_id_list' => [
  243. 'USER_FORM_INFO_FLAG_BIRTHDAY'
  244. ]
  245. ]
  246. ];
  247. return OfficialAccount::cardActivateUserForm($cardId, $requireForm, $optionFrom);
  248. }
  249. }