Sys.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use library\basic\BaseModel;
  5. use library\traits\JwtAuthModelTrait;
  6. use library\traits\ModelTrait;
  7. use think\Model;
  8. /**
  9. * @mixin \think\Model
  10. */
  11. class Sys extends Model
  12. {
  13. use ModelTrait;
  14. use JwtAuthModelTrait;
  15. /**
  16. * 保存数据
  17. * @param $save
  18. */
  19. public function saveSys($save) {
  20. $this->where('id',1)->save($save);
  21. }
  22. /**
  23. * 微信配置信息
  24. * @param type $data
  25. */
  26. public function saveWxConfig($data){
  27. $save=[
  28. "appid" => empty($data["appid"])?"":$data["appid"],
  29. "secret" => empty($data["secret"])?"":$data["secret"],
  30. "mchid" => empty($data["mchid"])?"":$data["mchid"],
  31. "apiv2key" => empty($data["apiv2key"])?"":$data["apiv2key"],
  32. "apiclient_key" => empty($data["apiclient_key"])?"":$data["apiclient_key"],
  33. "apiclient_cert" => empty($data["apiclient_cert"])?"":$data["apiclient_cert"],
  34. "notify_url" => empty($data["notify_url"])?"":$data["notify_url"],
  35. ];
  36. $this->where("id",1)->update(["wxconfig"=> serialize($save)]);
  37. }
  38. /**
  39. * 分享配置信息
  40. * @param type $data
  41. */
  42. public function saveShareConfig($data){
  43. $save=[
  44. "img" => empty($data["img"]) ?"":$data["img"],
  45. "title" => empty($data["title"])?"":$data["title"],
  46. "query" => empty($data["query"])?"":$data["query"],
  47. "content" => empty($data["content"])?"":$data["content"],
  48. "is_show" => isset($data["is_show"]) ? $data["is_show"] : 0,
  49. ];
  50. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】传参: ".json_encode($data, JSON_UNESCAPED_UNICODE)." \r\n", 8);
  51. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】处理后: ".json_encode($save, JSON_UNESCAPED_UNICODE)." \r\n", 8);
  52. // 检查 id=1 的记录是否存在
  53. $exists = $this->where("id", 1)->find();
  54. if (!$exists) {
  55. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】id=1 不存在,执行插入 \r\n", 8);
  56. $result = $this->insert([
  57. "id" => 1,
  58. "shareconfig" => serialize($save)
  59. ]);
  60. } else {
  61. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】id=1 存在,执行更新 \r\n", 8);
  62. // 使用 force() 强制更新,忽略数据是否变化
  63. $result = $this->where("id", 1)->strict(false)->update(["shareconfig" => serialize($save)]);
  64. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】旧值: ".$exists->shareconfig." \r\n", 8);
  65. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】新值: ".serialize($save)." \r\n", 8);
  66. }
  67. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】更新结果: ".$result." \r\n", 8);
  68. return $result;
  69. }
  70. /**
  71. * 邀请配置
  72. * @param type $data
  73. */
  74. public function saveInviteConfig($data){
  75. $save=[
  76. "img" => empty($data["img"]) ? "" : $data["img"],
  77. ];
  78. $this->where("id",1)->update(["inviteconfig"=> serialize($save)]);
  79. }
  80. /**
  81. * 积分配置信息
  82. * @param type $data
  83. */
  84. public function saveScoreConfig($data){
  85. $points_transformation = empty($data["points_transformation"]) ?"":$data["points_transformation"];
  86. $points_share = empty($data["points_share"])?"":$data["points_share"];
  87. $this->where("id",1)->update(["points_transformation"=> $points_transformation,'points_share'=>$points_share]);
  88. }
  89. /**
  90. * 获取配置详情
  91. * @return type
  92. */
  93. public function getDataInfo($code=""){
  94. $sys = $this->where("id", 1)->find();
  95. //微信配置
  96. $wxconfig=[
  97. "appid"=>"",
  98. "secret"=>"",
  99. "mchid"=>"",
  100. "apiv2key"=>"",
  101. "apiclient_key"=>"",
  102. "apiclient_cert"=>"",
  103. "notify_url"=>""
  104. ];
  105. if(!empty($sys->wxconfig)){
  106. $wxconfig = unserialize($sys->wxconfig);
  107. }
  108. //分享配置
  109. $shareconfig=[
  110. "img"=>"",
  111. "title"=>"",
  112. "query"=>"",
  113. "content"=>"",
  114. "is_show"=>"",
  115. ];
  116. if(!empty($sys->shareconfig)){
  117. $shareconfig = unserialize($sys->shareconfig);
  118. }
  119. //邀请配置
  120. $inviteconfig=[
  121. "img"=>"",
  122. ];
  123. if(!empty($sys->inviteconfig)){
  124. $inviteconfig = unserialize($sys->inviteconfig);
  125. }
  126. if($code=="weixin"){
  127. return $wxconfig;
  128. }
  129. if($code=="share"){
  130. return $shareconfig;
  131. }
  132. if($code=="invite"){
  133. return $inviteconfig;
  134. }
  135. //积分配置
  136. $scoreconfig=[
  137. "points_transformation"=>"",
  138. ];
  139. if(!empty($sys->points_transformation)){
  140. $scoreconfig['points_transformation'] = $sys->points_transformation;
  141. }
  142. if($code=="score"){
  143. return $scoreconfig;
  144. }
  145. $data = $sys->toArray();
  146. $data["wxconfig"] = $wxconfig;
  147. $data["shareconfig"] = $shareconfig;
  148. $data["inviteconfig"] = $inviteconfig;
  149. return $data;
  150. }
  151. /**
  152. * 获取微信配置
  153. * @return bool
  154. */
  155. public function getWeixinConfig(){
  156. $data = $this->getDataInfo("weixin");
  157. foreach($data as $k=>$v){
  158. if(empty($v)){
  159. return false;
  160. }
  161. }
  162. $return=[
  163. "APPID" => $data["appid"],
  164. "APPSECRET" => $data["secret"],
  165. "MCHID" => $data["mchid"],
  166. "ApiV2Key" => $data["apiv2key"],
  167. "ApiclientKey" => "file://".app()->getRootPath()."/".trim($data["apiclient_key"],"/"),
  168. "ApiclientCert" => "file://".app()->getRootPath()."/".trim($data["apiclient_cert"],"/"),
  169. "NOTIFY_URL" => $data["notify_url"],
  170. ];
  171. return $return;
  172. }
  173. }