Subscribe.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\common\service\subscribe\mini;
  10. use app\common\service\Service;
  11. use app\common\library\wechat\Factory;
  12. use app\common\models\order\Order as OrderModel;
  13. use app\common\models\order\OrderRefundment as OrderRefundmentModel;
  14. use app\common\models\order\OrderDelivery as OrderDeliveryModel;
  15. use app\common\models\Setting as SettingModel;
  16. use app\common\models\wechat\SubscribeMessage as SubscribeMessageModel;
  17. class Subscribe extends Service {
  18. /**
  19. * 退款通知
  20. */
  21. public static function refundNotice($openid,$order_no){
  22. if(!$config = self::getConfig("refund_notice")){
  23. return false;
  24. }
  25. if(!$refund = OrderRefundmentModel::where("order_no",$order_no)->find()){
  26. return false;
  27. }
  28. $data = [];
  29. foreach($config['attribute'] as $key=>$value){
  30. $field = isset($refund[$value['field']]) ? $refund[$value['field']] : "";
  31. if(in_array($value["field"],['create_time','dispose_time'])){
  32. $field = date("Y年m月d日 H:i:s",$field);
  33. }else if($value["field"] == 'pay_status'){
  34. $field = $refund['pay_status'] == 2 ? '退款成功' : '拒绝退款';
  35. }
  36. $data[$value["value"]] = [
  37. "value" => $field
  38. ];
  39. }
  40. try {
  41. Factory::mini()->subscribe_message->send($openid,$config["template_id"],$data,self::getMiniprogramState());
  42. return true;
  43. }catch (\Exception $ex){}
  44. return false;
  45. }
  46. /**
  47. * 配送通知
  48. */
  49. public static function deliveryNotice($openid,$delivery_id){
  50. if(!$config = self::getConfig("delivery_notice")){
  51. return false;
  52. }
  53. if(!$orderDelivery = OrderDeliveryModel::where("id",$delivery_id)->find()){
  54. return false;
  55. }
  56. if(!$order = OrderModel::where("id",$orderDelivery["order_id"])->find()){
  57. return false;
  58. }
  59. $orderDelivery["order_no"] = $order["order_no"];
  60. $data = [];
  61. foreach($config['attribute'] as $key=>$value){
  62. $field = isset($orderDelivery[$value['field']]) ? $orderDelivery[$value['field']] : "";
  63. if($value["field"] == "{{custom}}"){
  64. $field = "您的订单已发货";
  65. }
  66. $data[$value["value"]] = [
  67. "value" => $field
  68. ];
  69. }
  70. try {
  71. Factory::mini()->subscribe_message->send($openid,$config["template_id"],$data,self::getMiniprogramState());
  72. return true;
  73. }catch (\Exception $ex){}
  74. return false;
  75. }
  76. /**
  77. * 订单完成通知
  78. */
  79. public static function orderComplete($openid,$order_id){
  80. if(!$config = self::getConfig("order_complete")){
  81. return false;
  82. }
  83. if(!$order = OrderModel::where("id",$order_id)->find()){
  84. return false;
  85. }
  86. if($order['status'] != 5){
  87. return false;
  88. }
  89. $data = [];
  90. foreach($config['attribute'] as $key=>$value){
  91. $field = isset($order[$value['field']]) ? $order[$value['field']] : "";
  92. if(in_array($value["field"],['create_time','dispose_time'])){
  93. $field = date("Y年m月d日 H:i:s",$field);
  94. }else if($value["field"] == '{{custom}}'){
  95. $field = '完成交易';
  96. }
  97. $data[$value["value"]] = [ "value" => $field ];
  98. }
  99. try {
  100. Factory::mini()->subscribe_message->send($openid,$config["template_id"],$data,self::getMiniprogramState());
  101. return true;
  102. }catch (\Exception $ex){}
  103. return false;
  104. }
  105. /**
  106. * 订单支付成功通知
  107. */
  108. public static function orderPaySuccess($openid,$order_id){
  109. if(!$config = self::getConfig("order_pay_success")){
  110. return false;
  111. }
  112. if(!$order = OrderModel::where("id",$order_id)->find()){
  113. return false;
  114. }
  115. if($order['pay_status'] != 1){
  116. return false;
  117. }
  118. $data = [];
  119. foreach($config['attribute'] as $key=>$value){
  120. $field = isset($order[$value['field']]) ? $order[$value['field']] : "";
  121. if(in_array($value["field"],['create_time','pay_time'])){
  122. $field = date("Y年m月d日 H:i:s",$field);
  123. }else if($value["field"] == 'pay_status'){
  124. $field = '支付成功';
  125. }
  126. $data[$value["value"]] = [ "value" => $field ];
  127. }
  128. try {
  129. Factory::mini()->subscribe_message->send($openid,$config["template_id"],$data,self::getMiniprogramState());
  130. return true;
  131. }catch (\Exception $ex){}
  132. return false;
  133. }
  134. /**
  135. * 获取小程序状态
  136. * @return mixed|string
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\DbException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. */
  141. public static function getMiniprogramState(){
  142. $config = SettingModel::getArrayData("wemini_base");
  143. return isset($config["mini_status"]) ? $config["mini_status"] : "trial";
  144. }
  145. /**
  146. * 获取配置
  147. * @param string $sign
  148. * @return array|false
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\DbException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. */
  153. public static function getConfig($sign=""){
  154. $result = SubscribeMessageModel::where("sign",$sign)->where("status",0)->find();
  155. if(empty($result) || empty($result["content"])){
  156. return false;
  157. }
  158. if($result["status"]){
  159. return false;
  160. }
  161. $attr = json_decode($result["content"],true);
  162. if(is_null($attr)){
  163. return false;
  164. }
  165. $array = [];
  166. foreach($attr["name"] as $k=>$v){
  167. $value = str_replace(["{{","}}",".DATA"],["","",""],$attr["value"][$k]);
  168. $array[$k] = [
  169. "name"=>$v,
  170. "field"=>$attr["field"][$k],
  171. "value"=>$value,
  172. ];
  173. }
  174. return [ "template_id"=>$result["temp_id"], "attribute"=>$array ];
  175. }
  176. }